<%@ tag import="su.mvc.action.ActionSupport" %>
<%@ tag import="su.mvc.service.UserService" %>
<%@ tag import="su.mvc.service.gui.GuiService" %>
<%@ tag import="com.googlecode.objectify.Objectify" %>
<%@ tag language="java" pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ attribute name="authClass" required="false" %>

<c:if test="${empty authClass}"><c:set var="authClass" value="auth_block" /></c:if>

<%

    UserService userService = ((GuiService)request.getAttribute(ActionSupport.GUI_SERVICE)).getUserService();
    userService.setObjectify((Objectify)request.getAttribute(ActionSupport.OBJECTIFY));
    String currentPage = (String)request.getAttribute(ActionSupport.CURRENT_PAGE);
    boolean isUserAuthorized = userService.isUserAuthorized();
    jspContext.setAttribute("isLoggedIn", isUserAuthorized);
    if (isUserAuthorized) {
        jspContext.setAttribute("isAdmin", userService.isUserAdmin());
        jspContext.setAttribute("logoutUrl", userService.getLogoutPage(currentPage));
        jspContext.setAttribute("nick", userService.getUserNick());
    } else {
        jspContext.setAttribute("loginUrl", userService.getLoginPage(currentPage));
    }

%>

<p class="${authClass}">

    <fmt:bundle basename="global">

        <c:choose>
            <c:when test="${isLoggedIn}">
                Hey, ${nick}! <a href="${logoutUrl}"><fmt:message key="application.logout" /></a>
                <c:if test="${isAdmin}"><br /><b style="color: #DD4B39"><fmt:message key="application.admin" /></b></c:if>
            </c:when>
            <c:otherwise>
                <a href="${loginUrl}"><fmt:message key="application.login" /></a>
            </c:otherwise>
        </c:choose>

    </fmt:bundle>


</p>
